package com.ipzoe.web.controller.cms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ipzoe.common.core.domain.entity.SysUser;
import com.ipzoe.common.utils.SecurityUtils;
import com.ipzoe.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.cms.domain.Banner;
import com.ipzoe.cms.service.IBannerService;
import com.ipzoe.common.utils.poi.ExcelUtil;
import com.ipzoe.common.core.page.TableDataInfo;

/**
 * bannerController
 *
 * @author eric
 * @date 2020-08-13
 */
@RestController
@RequestMapping("/cms/banner")
public class BannerController extends BaseController {

    @Autowired
    private IBannerService iBannerService;

    /**
     * 查询banner列表
     */
    @PreAuthorize("@ss.hasPermi('cms:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(Banner banner) {
        startPage();
        LambdaQueryWrapper<Banner> lqw = new LambdaQueryWrapper<Banner>();
        if (StringUtils.isNotBlank(banner.getName())) {
            lqw.like(Banner::getName, banner.getName());
        }
        if (banner.getUseFlag() != null) {
            lqw.eq(Banner::getUseFlag, banner.getUseFlag());
        }
        lqw.orderByDesc(Banner::getCreateTime);
        List<Banner> list = iBannerService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出banner列表
     */
    @PreAuthorize("@ss.hasPermi('cms:banner:export')")
    @Log(title = "banner", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Banner banner) {
        LambdaQueryWrapper<Banner> lqw = new LambdaQueryWrapper<Banner>(banner);
        List<Banner> list = iBannerService.list(lqw);
        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
        return util.exportExcel(list, "banner");
    }

    /**
     * 获取banner详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:banner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iBannerService.getById(id));
    }

    /**
     * 新增banner
     */
    @PreAuthorize("@ss.hasPermi('cms:banner:add')")
    @Log(title = "banner", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Banner banner) {
        banner.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iBannerService.save(banner) ? 1 : 0);
    }

    /**
     * 修改banner
     */
    @PreAuthorize("@ss.hasPermi('cms:banner:edit')")
    @Log(title = "banner", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Banner banner) {
        banner.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iBannerService.updateById(banner) ? 1 : 0);
    }

    /**
     * 删除banner
     */
    @PreAuthorize("@ss.hasPermi('cms:banner:remove')")
    @Log(title = "banner", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iBannerService.removeByIds(Arrays.asList(ids),SecurityUtils.getUsername()) ? 1 : 0);
    }

    /**
     * 上下架
     */
    @PreAuthorize("@ss.hasPermi('cms:banner:edit')")
    @Log(title = "banner", businessType = BusinessType.UPDATE)
    @PutMapping("/changeUseFlag")
    public AjaxResult changeUseFlag(@RequestBody Banner banner)
    {
        banner.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iBannerService.updateById(banner) ? 1 : 0);
    }
}
